package com.madarasan.resorts;

import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import com.madarasan.SkiDroid.MyApplication;
import com.madarasan.SkiDroid.R;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ShowResortList extends ListActivity {

	DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
	ArrayList<ArrayList<String>> resorts = new ArrayList<ArrayList<String>>();
	String path = "http://dragos.madarasan.com/ski/ski_test.xml";

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		Log.i("ShowResortList", "onCreate called");

		Log.i("ShowResortList", "load feed");

		new DownloadFilesTask().execute();

	}

	private class DownloadFilesTask extends AsyncTask<Void, Integer, Integer> {

		public ProgressDialog dialog;

		@Override
		protected void onPreExecute() {

			dialog = new ProgressDialog(ShowResortList.this);
			dialog.setCancelable(false);
			dialog.setMessage(getApplicationContext().getResources().getString(R.string.dialog_loading));
			dialog.setMax(100);
			dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			dialog.show();
			// Log.d("ShowResortList->onPreExecute","shown diag");

		}

		@Override
		protected Integer doInBackground(Void... unused) {

			Log.i("ShowResortList", "reading feed");

			Integer results = 0;

			try {

				domFactory.setNamespaceAware(true);
				DocumentBuilder builder = domFactory.newDocumentBuilder();
				XPath xpath = XPathFactory.newInstance().newXPath();

				Log.i("ShowResortList", "Checking before parse");
				Document doc = builder
						.parse("http://dragos.madarasan.com/ski/resorts.xml");
				Log.i("ShowResortList", "Checking after parse");

				XPathExpression expr = xpath.compile("/resorts/item");

				Log.i("ShowResortList", "Checking before evaluation");
				NodeList nodes = (NodeList) expr.evaluate(doc,
						XPathConstants.NODESET);
				Log.i("ShowResortList", "Checking after evaluation");

				for (Integer i = 0; i < nodes.getLength(); i++) {
					results = nodes.getLength();

					NodeList items = nodes.item(i).getChildNodes();
					ArrayList<String> provider = new ArrayList<String>();
					provider.add(items.item(1).getTextContent()); // add the
																	// title to
																	// titles
																	// array
					provider.add(items.item(3).getTextContent()); // add the
																	// link to
																	// titles
																	// array
					provider.add(items.item(5).getTextContent()); // add the
																	// description
																	// to titles
																	// array
					provider.add(items.item(7).getTextContent()); // add the
																	// tempmax
																	// to titles
																	// array
					provider.add(items.item(9).getTextContent()); // add the
																	// tempmin
																	// to titles
																	// array
					provider.add(items.item(11).getTextContent()); // add the
																	// wind to
																	// titles
																	// array
					provider.add(items.item(13).getTextContent()); // add the
																	// snow to
																	// titles
																	// array
					provider.add(items.item(15).getTextContent()); // add the
																	// weather
																	// to titles
																	// array
					provider.add(items.item(17).getTextContent()); // add the
																	// pubdate
																	// to titles
																	// array
					resorts.add(provider);

					publishProgress((int) ((i / (float) results) * 100));

				}

			}

			catch (Exception e) {
				e.printStackTrace();
			}

			Log.i("ShowResortList", "exiting doInBackground");
			// return results;
			return 1;

		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			// Log.i("ShowResortList", "updating progress to "+values[0]);
			dialog.setProgress((int) values[0]);

		};

		@Override
		protected void onPostExecute(Integer result) {
			// Log.i("ShowResortList onPostExecute",
			// " hiding dialog, calling loadFeed");
			dialog.hide();
			loadFeed();
		}

	}

	public void loadFeed() {

		try {
			List<String> resortNames = new ArrayList<String>();
			for (Integer i = 0; i < resorts.size(); i++) {

				resortNames.add(resorts.get(i).get(0).toString());
			}

			setContentView(R.layout.cities);

			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
					R.layout.row, resortNames);
			Log.i("ShowResortList loadfeed", "updating the view");
			this.setListAdapter(adapter);

			final ListView lv = getListView();
			lv.setItemsCanFocus(false);
			lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		} catch (Throwable t) {
			Log.e("ProviderList", t.getMessage(), t);
		}
	}

	protected void onListItemClick(ListView l, View v, int position, long id) {

		String title = resorts.get(position).get(0).toString();
		String link = resorts.get(position).get(1).toString();
		String description = resorts.get(position).get(2).toString();
		String tempMax = resorts.get(position).get(3).toString();
		String tempMin = resorts.get(position).get(4).toString();
		String wind = resorts.get(position).get(5).toString();
		String snow = resorts.get(position).get(6).toString();
		String weather = resorts.get(position).get(7).toString();
		String date = resorts.get(position).get(8).toString();

		((MyApplication) this.getApplication()).MyApplication(title, link,
				description, tempMax, tempMin, wind, snow, weather, date);
		Log.i("ShowResortList", description);

		((MyApplication) this.getApplication()).setResortSelected(true);
		Log.i("ResortList", "adding info to my application");

		Intent ShowResortGenInfoIntent = new Intent(this,
				ShowResortGeneralInfo.class);
		startActivity(ShowResortGenInfoIntent);

	}

}
